/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.ui.dialogs;


import java.util.Iterator;

import org.eclipse.gef.commands.CommandStack;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.uml2.uml.ChangeEvent;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.LiteralString;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.commands.UMLElementCreateCommand;
import com.cea.papyrus.core.editor.CommandStackUtils;

/**
 * Dialog to select an ChangeEvent in a Package.
 * 
 * @author David Servat
 * @since 5. apr. 2007
 */
public class PackageChangeEventTreeSelectionDialog extends Dialog{

	/**
	 * The Package for which we want to show a list of events.
	 */
	protected org.eclipse.uml2.uml.Package package_;

	/**
	 * Chosen event.
	 */
	protected ChangeEvent changeEvent;

	/**
	 * The selection tree.
	 */
	protected Tree tree;

	/**
	 * Button to create new operation
	 */
	protected Button newButton;

	/**
	 * Label for description
	 */
	protected Label label;

	/**
	 * Constructor with parent shell and package.
	 * 
	 * @param parent the Shell.
	 * @param package_ the Package.
	 */
	public PackageChangeEventTreeSelectionDialog(Shell parent, org.eclipse.uml2.uml.Package package_){
		super(parent);
		// add the resize ability to the window
		setShellStyle(SWT.RESIZE | super.getShellStyle());
		this.package_ = package_;
	}

	/**
	 * Creates the dialog area.
	 * 
	 * @param parent composite.
	 * 
	 * @return control Control.
	 */
	protected Control createDialogArea(Composite parent){
		Composite composite = (Composite)super.createDialogArea(parent);

		label = new Label(composite, SWT.NONE);
		label.setText("Select an existing ChangeEvent or create a new one:");
		label.setLayoutData(new GridData());

		newButton = new Button(composite, SWT.NONE);
		newButton.setText("New ChangeEvent");
		newButton.setLayoutData(new GridData(200, 25));

		tree = new Tree(composite, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL);

		Iterator iter = package_.allOwnedElements().iterator();
		while(iter.hasNext()){
			Element elt = (Element)iter.next();
			if(elt instanceof ChangeEvent){
				TreeItem item = new TreeItem(tree, SWT.NONE);
				ChangeEvent chg = (ChangeEvent)elt;
				if((chg.getChangeExpression() != null) && (chg.getChangeExpression() instanceof LiteralString))
					item.setText(chg.getName()+" -> when "+chg.getChangeExpression().stringValue());
				else
					item.setText(chg.getName());
				item.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(chg));
				item.setData(chg);
			}
		}
		tree.setLayoutData(new GridData(550, 300));

		tree.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e){
				TreeItem[] selection = tree.getSelection();
				if(selection[0].getData() instanceof ChangeEvent){
					changeEvent = (ChangeEvent)selection[0].getData();
				}
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});

		newButton.addMouseListener(new MouseListener(){
			public void mouseDoubleClick(MouseEvent e) {
			}

			public void mouseDown(MouseEvent e) {
			}

			public void mouseUp(MouseEvent e) {

				InputStringDialog dialog = new InputStringDialog(getShell(), "Enter change expression");
				int result = dialog.open();

				//check the result
				if(result == IDialogConstants.OK_ID){
					//declared final so as to be used in inner classes
					final String expression = dialog.getValue(); 
					if(expression != null){
						// creation command
						final UMLElementCreateCommand command = new UMLElementCreateCommand(ChangeEvent.class, package_);
		
						CommandStack commandStack = CommandStackUtils.getCommandStack();
						if(commandStack!= null) {
							commandStack.execute(command);
						}
		
						ChangeEvent newEvent = (ChangeEvent)command.getElement();
						LiteralString ls = UMLFactory.eINSTANCE.createLiteralString();
						ls.setValue(expression);
						newEvent.setChangeExpression(ls);
						
						// add the new event
						TreeItem eventItem = new TreeItem(tree, SWT.SINGLE);
						eventItem.setText(newEvent.getName()+" -> when "+expression);
						eventItem.setData(newEvent);
						eventItem.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(newEvent));
		
						tree.setSelection(eventItem);
						changeEvent = (ChangeEvent)eventItem.getData();
					}
				}
			}
		
		});

		return composite;
	}

	/**
	 * Returns the associated ChangeEvent.
	 * 
	 * @return changeEvent ChangeEvent
	 */
	public ChangeEvent getResult(){
		return changeEvent;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
	 */
	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText(package_.getName()+": ChangeEvent Selection");
	}

}
